﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Runtime.Serialization;

namespace QbservableCore.Serialization
{
    [DataContract]
    public class ConditionalNode : ExpRepresentationBase
    {
        [DataMember]
        public ExpRepresentationBase Test { get; set; }

        [DataMember]
        public ExpRepresentationBase TruePart { get; set; }

        [DataMember]
        public ExpRepresentationBase FalsePart { get; set; }

        protected internal override Expression ToExpression(ExepressionBuilderCache exepressionBuilderCache)
        {
            return Expression.Condition(Test.ToExpression(exepressionBuilderCache), TruePart.ToExpression(exepressionBuilderCache), FalsePart.ToExpression(exepressionBuilderCache));
        }

        protected override void TraverseChildren(Func<ExpRepresentationBase, ExpRepresentationBase> action)
        {
            Test = Test.Traverse(action);
            TruePart = TruePart.Traverse(action);
            FalsePart = FalsePart.Traverse(action);
        }

        public override string ToString()
        {
            return string.Format("{0} ? {1} : {2}", Test, TruePart, FalsePart);
        }
    }
}